package com.cloud.service.impl.login;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cloud.constant.AuthConstants;
import com.cloud.dao.user.UserMapper;
import com.cloud.exception.ValidateException;
import com.cloud.pojo.user.UserInfo;
import com.cloud.service.LoginService;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @author: TianXiang
 * @description:
 * @date: 2021/2/24
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserMapper userMapper;

    @Override
    public String register(UserInfo mo) {
        QueryWrapper userInfoQuery = new QueryWrapper();
        userInfoQuery.eq("username", mo.getUsername());
        UserInfo userInfo = userMapper.selectOne(userInfoQuery);
        if(null != userInfo) {
            throw new ValidateException("用户名存在!");
        }
        if(StringUtils.isBlank(mo.getFingerprintCode())) {
            throw new ValidateException("浏览器指纹码为空!");
        }

        // 判断数据库里是否存在此浏览器指纹码
        QueryWrapper fingerprintQuery = new QueryWrapper();
        fingerprintQuery.eq("fingerprint_code", mo.getFingerprintCode());
        List<UserInfo> fingerprintUserList = userMapper.selectList(fingerprintQuery);
        if(!Collections.isEmpty(fingerprintUserList)) {
            throw new ValidateException("一台设备只能注册一个帐号! 请使用此帐号进行登录: " + fingerprintUserList.get(0).getUsername());
        }
        // 用户信息入库
        mo.setLoginCount(1)
                .setPassword(new BCryptPasswordEncoder().encode(mo.getPassword()))
                .setRole(AuthConstants.VIP)
                .setFingerprintCode(mo.getFingerprintCode())
                .setSynchronizeFingerprintTime(new Date())
                .setSynchronizeFingerprintTimeLast(new Date());
        userMapper.insert(mo);
        return mo.getFingerprintCode();
    }

    @Override
    public UserInfo findUserByUsername(String username) {
        QueryWrapper query = new QueryWrapper();
        query.eq("username", username);
        UserInfo userInfo = userMapper.selectOne(query);
        return userInfo;
    }

    @Override
    public void expirePermission() {
        QueryWrapper query = new QueryWrapper();
        query.eq("role", AuthConstants.VIP);
        query.ge("expire_time", DateUtil.beginOfDay(new Date()));
        query.le("expire_time", DateUtil.endOfDay(new Date()));
        List<UserInfo> userInfoList = userMapper.selectList(query);
        userInfoList = userInfoList.stream().filter(item -> null != item.getExpireTime()).collect(Collectors.toList());
        for (UserInfo userInfo : userInfoList) {
            UpdateWrapper userUpdate = new UpdateWrapper();
            userUpdate.eq("username", userInfo.getUsername());
            userUpdate.set("role", AuthConstants.VISITOR);
            userMapper.update(null, userUpdate);
        }
    }
}
